#!/bin/sh

#/*`````````````````````````````````````````````````\
#|  Constansts                                      |
#\.................................................*/

tvs="/patients8/home8/tomv/scripts/deformable"
ivec="${tvs}/invertVectormap.sh"
catv="${tvs}/catVectormap.sh"
compDvh="/patients8/home8/tomv/scripts/dvh/computeDvh.sh"
cpwd=`pwd`
PINDIR="/adacp3u1/PrimaryPatientData/NewPatients/Institution_1/Mount_0"
calcAll=0
exclude=0

#/*`````````````````````````````````````````````````\
#|  Helper functions loaded from externally         |
#\.................................................*/

. ${tvs}/compDose.sh

#/*`````````````````````````````````````````````````\
#|  Error functions                                 |
#\.................................................*/

usage()
{
    echo "Usage :"
    echo "janus.sh <patient directory>"
    echo "         -pin1 <pinnacle patient 1> -pin1Img <pinnacle patient 1 primary imageset ID> -plan1 <pinnacle plan number 1>"
    echo "         -pin2 <pinnacle patient 2> -pin2Img <pinnacle patient 2 primary imageset ID> -plan2 <pinnacle plan number 2>"
    echo "         -pin4 <pinnacle patient 4> -pin4Img <pinnacle patient 4 primary imageset ID> -plan4 <pinnacle plan number 4>"
    exit 1
}

wrong()
{
        echo "Something went wrong:"
        if [ "x$1" != "x" ]; then
                echo "$1"
        fi
        #exit 1
}

#/*`````````````````````````````````````````````````\
#|  Checks on the directory                         |
#\.................................................*/

if [ "x$1" = "x" ]; then
	usage
fi

if [ ! -e "$1" ]; then
	wrong "input directory does not exist"
	exit 1
fi

cd "$1"

#
# Make deformed dose sum on CT1 CT3 CT4.
#

#/*`````````````````````````````````````````````````\
#|  Configuration.                                  |
#\.................................................*/
# Patient dir
ptdir="$1"
# First plan
d1='Phase_I/manual'
p1='IMRT1_CT1'
s1='sum_fx_270cGy'
n1='5'
i1='CTs/plan_im_prim/shifted'
w1='wt_fx_270cGy'

# Second plan
d2='Phase_II/deformed'
p2='IMRT2_CT2'
s2='sum_fx_216cGy'
n2='5'
i2='CTs/plan_im_prim/shifted'
w2='wt_fx_216cGy'

# Third plan
d3='Phase_III/deformed'
p3='IMRT3_CT3'
s3='sum_fx_216cGy'
n3='5'
i3='CTs/plan_im_prim/shifted'
w3='wt_fx_216cGy'

# Fourth plan
d4='Phase_IV/deformed'
p4='IMRT'
#s4='sum_fx'
#n4='0'
i4='CTs/plan_im_prim'

# Janus plan 2 on CT1
d5=${d1}
p5='IMRT2_janus_CT1'
s5=${s2}
n5='5'
i5=${i1}
w5=${w2}

# Janus plan 3 on CT2
d6=${d2}
p6='IMRT3_janus_CT2'
s6=${s3}
n6='5'
i6=${i2}
w6=${w3}

# Janus plan 3 on CT4
d7=${d4}
p7='IMRT3_janus_CT4'
s7=${s3}
n7='5'
i7=${i4}
w7=${w3}

# Janus output plans
jodir1="${d5}/${p5}"
jodir2="${d6}/${p6}"
jodir3="${d7}/${p7}"

# Janus summed output
odir4="${d4}/IMRT_janus_sum_CT4"

# Input vectormaps provided by an external engine.
#v1to2="${d2}/abas/ct1_2_ct2_deformations/adaptive_ct/CT1_Elb_RTS"
#v2to3="${d3}/abas/ct2_2_ct3_deformations/adaptive_ct/CT2_Elb_RTS"
#v3to4="${d4}/abas/ct3_2_ct4_deformations/adaptive_ct/CT3_ELB_RTS"

# Output vectormaps directory
dv="Vectormaps"

# Input vectormaps provided by an external engine.
v1to2="$dv/CT1toCT2"
v2to3="$dv/CT2toCT3"
v3to4="$dv/CT3toCT4"

#/*`````````````````````````````````````````````````\
#|  Processing of the script arguments              |
#\.................................................*/
# Pinnacle plan on CT1
if [ "x${2}" != "x-pin1" ]; then
    echo "Expected pinnacle patient number for CT1"
    usage
fi
if [ "x${4}" != "x-pin1Img" ]; then
    echo "Expected pinnacle primary imageset ID for CT1"
    usage
fi
if [ "x${6}" != "x-plan1" ]; then
    echo "Expected pinnacle plan number for CT1"
    usage
fi
pin1Id=${3}
pin1Img=${5}
pin1Plan=${7}

# Pinnacle plan on CT2
if [ "x${8}" != "x-pin2" ]; then
    echo "Expected pinnacle patient number for CT2"
    usage
fi
if [ "x${10}" != "x-pin2Img" ]; then
    echo "Expected pinnacle primary imageset ID for CT2"
    usage
fi
if [ "x${12}" != "x-plan2" ]; then
    echo "Expected pinnacle plan number for CT2"
    usage
fi
pin2Id=${9}
pin2Img=${11}
pin2Plan=${13}

# Pinnacle plan on CT4
if [ "x${14}" != "x-pin4" ]; then
    echo "Expected pinnacle patient number for CT4"
    usage
fi
if [ "x${16}" != "x-pin4Img" ]; then
    echo "Expected pinnacle primary imageset ID for CT4"
    usage
fi
if [ "x${18}" != "x-plan4" ]; then
    echo "Expected pinnacle plan number for CT4"
    usage
fi
pin4Id=${15}
pin4Img=${17}
pin4Plan=${19}

#/*`````````````````````````````````````````````````\
#|  Checks on the patient directory structure       |
#\.................................................*/

if [ ! -e "${d1}" ]; then
	wrong "${d1} directory does not exist"
fi

if [ ! -e "${d1}/${p1}" ]; then
	wrong "${d1}/${p1} directory does not exist"
fi

if [ ! -e "${d1}/${p1}/${s1}" ]; then
	wrong "${d1}/${p1}/${s1} : sum file does not exist"
fi

if [ ! -e "${d1}/${i1}" ]; then
	wrong "${d1}/${i1} : imageset does not exist"
fi

if [ ! -e "${d2}" ]; then
	wrong "${d2} directory does not exist"
fi

if [ ! -e "${d2}/${p2}" ]; then
	wrong "${d2}/${p2} directory does not exist"
fi

if [ ! -e "${d2}/${p2}/${s2}" ]; then
	wrong "${d2}/${p2}/${s2} : sum file does not exist"
fi

if [ ! -e "${d2}/${i2}" ]; then
	wrong "${d2}/${i2} : imageset does not exist"
fi

if [ ! -e "${d3}" ]; then
	wrong "${d3} directory does not exist"
fi

if [ ! -e "${d3}/${p3}" ]; then
	wrong "${d3}/${p3} directory does not exist"
fi

if [ ! -e "${d3}/${p3}/${s3}" ]; then
	wrong "${d3}/${p3}/${s3} : sum file does not exist"
fi

if [ ! -e "${d3}/${i3}" ]; then
	wrong "${d3}/${i3} : imageset does not exist"
fi

#/*`````````````````````````````````````````````````\
#|  Checks on the vectorfields                      |
#\.................................................*/

if [ ! -e "${v1to2}.mhd" ]; then
	wrong "${v1to2} mhd file does not exist"
fi

if [ ! -e "${v1to2}.raw" ]; then
	wrong "${v1to2} raw file does not exist"
fi

if [ ! -e "${v2to3}.mhd" ]; then
	wrong "${v2to3} mhd file does not exist"
fi

if [ ! -e "${v2to3}.raw" ]; then
	wrong "${v2to3} raw file does not exist"
fi

if [ ! -e "${v3to4}.mhd" ]; then
	wrong "${v3to4} mhd file does not exist"
fi

if [ ! -e "${v3to4}.raw" ]; then
	wrong "${v3to4} raw file does not exist"
fi


# The inverted and concatednated vectormaps that are needed for further computation
v3to2="${dv}/CT3toCT2"
v2to1="${dv}/CT2toCT1"
v3to1="${dv}/CT3toCT1"
v1to3="${dv}/CT1toCT3"
v4to3="${dv}/CT4toCT3"
v4to2="${dv}/CT4toCT2"
v4to1="${dv}/CT4toCT1"
if [ "${calcAll}" = "1" ]; then
    ${ivec} "${v3to2}" "${d3}/${i3}" "${v2to3}"
    ${ivec} "${v2to1}" "${d2}/${i2}" "${v1to2}"
    ${catv} "${v3to1}" "${v3to2}" "${v2to1}"
    ${catv} "${v1to3}" "${v1to2}" "${v2to3}"
    ${ivec} "${v4to3}" "${d4}/${i4}" "${v3to4}"
    ${catv} "${v4to2}" "${v4to3}" "${v3to2}"
    ${catv} "${v4to1}" "${v4to2}" "${v2to1}"
fi

if [ ! -e "${v3to2}.mhd" ]; then
        wrong "${v3to2} mhd file does not exist"
fi
if [ ! -e "${v3to2}.raw" ]; then
        wrong "${v3to2} raw file does not exist"
fi

if [ ! -e "${v2to1}.mhd" ]; then
        wrong "${v2to1} mhd file does not exist"
fi
if [ ! -e "${v2to1}.raw" ]; then
        wrong "${v2to1} raw file does not exist"
fi
if [ ! -e "${v3to1}.mhd" ]; then
        wrong "${v3to1} mhd file does not exist"
fi
if [ ! -e "${v3to1}.raw" ]; then
        wrong "${v3to1} raw file does not exist"
fi
if [ ! -e "${v1to3}.mhd" ]; then
        wrong "${v1to3} mhd file does not exist"
fi
if [ ! -e "${v1to3}.raw" ]; then
        wrong "${v1to3} raw file does not exist"
fi
if [ ! -e "${v4to3}.mhd" ]; then
        wrong "${v4to3} mhd file does not exist"
fi
if [ ! -e "${v4to3}.raw" ]; then
        wrong "${v4to3} raw file does not exist"
fi
if [ ! -e "${v4to2}.mhd" ]; then
        wrong "${v4to2} mhd file does not exist"
fi
if [ ! -e "${v4to2}.raw" ]; then
        wrong "${v4to2} raw file does not exist"
fi
if [ ! -e "${v4to1}.mhd" ]; then
        wrong "${v4to1} mhd file does not exist"
fi
if [ ! -e "${v4to1}.raw" ]; then
        wrong "${v4to1} raw file does not exist"
fi

#/*`````````````````````````````````````````````````\
#|  Transfer plan 2 to CT1                          |
#\.................................................*/
mkdir -vp ${jodir1} || wrong "Can not create ${jodir1}"
mkdir -vp ${jodir1}/b || wrong "Can not create ${jodir1}/b"
mkdir -vp ${jodir1}/v || wrong "Can not create ${jodir1}/v"
mkdir -vp ${jodir1}/g || wrong "Can not create ${jodir1}/g"
mkdir -vp ${jodir1}/dvh || wrong "Can not create ${jodir1}/dvh"

cp -vrp ${ptdir}/${d5}/a ${jodir1}/a || wrong "Can not copy anastructs from ${d5} to ${jodir1}"
cp -vp ${ptdir}/${d5}/patient_name ${jodir1}/patient_name || wrong "Can not copy patient_name from ${d5} to ${jodir1}"
cp -vp ${ptdir}/${d5}/plan_im ${jodir1}/plan_im || wrong "Can not copy plan_im from ${d5} to ${jodir1}"
cp -vp ${ptdir}/${d1}/${p1}/grid ${jodir1}/grid || wrong "Can not copy grid from ${d1}/${p1}/ to ${jodir1}/grid"

echo "Deforming isocenter of the beams of CT2 to CT1"
for i in `ls "${d2}/${p2}/b/"`; do
    echo "${d2}/${p2}/b/${i}"
    deformBeam "${jodir1}/b/${i}" "${v2to1}" "${d2}/${p2}/b/${i}" \
               -ct1 "${d1}/${i1}" -pi1 "${d1}/${p1}/plan_im" \
               -ct2 "${d2}/${i2}" -pi2 "${d2}/${p2}/plan_im"
done

echo "Filling anastructs with points"
for i in `ls "${jodir1}"/a`; do
    fill_anastruct_with_points -a "${jodir1}/a/${i}" -p "${jodir1}/dvh/${i}.pts" -c 10000
done

# transfer of CT2 to janus plan
echo "Transfer of CT2 based data to janus plan on CT1"
cp -vp "${d2}/${p2}/${w2}" "${jodir1}/${w5}" || wrong "Can not copy ${d2}/${p2}/${w2} from plan 2 to ${jodir1}/${w5}"
cp -vp "${jodir1}/${w5}" "${jodir1}/wt" || wrong "Can not copy${jodir1}/${w5} from plan 2 to ${jodir1}/wt"



#/*`````````````````````````````````````````````````\
#|  Transfer plan 3 to CT2                          |
#\.................................................*/
mkdir -vp ${jodir2} || wrong "Can not create ${jodir2}"
mkdir -vp ${jodir2}/b || wrong "Can not create ${jodir2}/b"
mkdir -vp ${jodir2}/v || wrong "Can not create ${jodir2}/v"
mkdir -vp ${jodir2}/g || wrong "Can not create ${jodir2}/g"
mkdir -vp ${jodir2}/dvh || wrong "Can not create ${jodir2}/dvh"

cp -vrp ${ptdir}/${d6}/a ${jodir2}/a || wrong "Can not copy anastructs from ${d6} to ${jodir2}"
cp -vp ${ptdir}/${d6}/patient_name ${jodir2}/patient_name || wrong "Can not copy patient_name from ${d6} to ${jodir2}"
cp -vp ${ptdir}/${d6}/plan_im ${jodir2}/plan_im || wrong "Can not copy plan_im from ${d6} to ${jodir2}"
cp -vp ${ptdir}/${d2}/${p2}/grid ${jodir2}/grid || wrong "Can not copy grid from ${d2}/${p2}/ to ${jodir2}/grid"

echo "Deforming isocenter of the beams of CT3 to CT2"
for i in `ls "${d3}/${p3}/b/"`; do
    echo "${d3}/${p3}/b/${i}"
    deformBeam "${jodir2}/b/${i}" "${v3to2}" "${d3}/${p3}/b/${i}" \
               -ct1 "${d2}/${i2}" -pi1 "${d2}/${p2}/plan_im" \
               -ct2 "${d3}/${i3}" -pi2 "${d3}/${p3}/plan_im"
done

echo "Filling anastructs with points"
for i in `ls "${jodir2}"/a`; do
    fill_anastruct_with_points -a "${jodir2}/a/${i}" -p "${jodir2}/dvh/${i}.pts" -c 10000
done

# transfer of CT3 to janus plan 2
echo "Transfer of CT3 based data to janus plan on CT2"
cp -vp "${d3}/${p3}/${w3}" "${jodir2}/${w6}" || wrong "Can not copy ${d3}/${p3}/${w3} from plan 3 to ${jodir2}/${w6}"
cp -vp "${jodir2}/${w6}" "${jodir2}/wt" || wrong "Can not copy ${jodir2}/${w6} from plan 3 to ${jodir2}/wt"



#/*`````````````````````````````````````````````````\
#|  Transfer plan 3 to CT4                          |
#\.................................................*/
mkdir -vp "${jodir3}" || wrong "Can not create ${jodir3}"
mkdir -vp "${jodir3}/b" || wrong "Can not create ${jodir3}/b"
mkdir -vp "${jodir3}/v" || wrong "Can not create ${jodir3}/v"
mkdir -vp "${jodir3}/g" || wrong "Can not create ${jodir3}/g"
mkdir -vp "${jodir3}/dvh" || wrong "Can not create ${jodir3}/dvh"

cp -vrp "${ptdir}/${d7}/a" "${jodir3}/a" || wrong "Can not copy anastructs from ${d7} to ${jodir3}"
cp -vp "${ptdir}/${d7}/patient_name" "${jodir3}/patient_name" || wrong "Can not copy patient_name from ${d7} to ${jodir3}"
cp -vp "${ptdir}/${d7}/plan_im" "${jodir3}/plan_im" || wrong "Can not copy plan_im from ${d7} to ${jodir3}"
if [ ! -e "${ptdir}/${d4}/grid" ]; then
    echo "Creating a grid around the skin"
    grid_around_ana "${d4}/grid" "${d4}/a/skin" -rx 0.4 -ry 0.4 -rz 0.4
fi
cp -vp "${ptdir}/${d4}/grid" "${jodir3}/grid" || wrong "Can not copy grid from ${d4} to ${jodir3}/grid"

echo "Deforming isocenter of the beams of CT3 to CT4"
for i in `ls "${d3}/${p3}/b/"`; do
    echo "${d3}/${p3}/b/${i}"
    deformBeam "${jodir3}/b/${i}" "${v3to4}" "${d3}/${p3}/b/${i}" \
               -ct1 "${d3}/${i3}" -pi1 "${d3}/${p3}/plan_im" \
               -ct2 "${d4}/${i4}" -pi2 "${d4}/plan_im"
done

# transfer of CT3 to janus plan 3
echo "Transfer of CT3 based data to janus plan on CT2"
cp -vp "${d3}/${p3}/${w3}" "${jodir3}/${w7}" || wrong "Can not copy ${d3}/${p3}/${w3} from plan 3 to ${jodir3}/${w7}"
cp -vp "${jodir3}/${w7}" "${jodir3}/wt" || wrong "Can not copy ${jodir3}/${w7} from plan 3 to ${jodir3}/wt"


#/*`````````````````````````````````````````````````\
#|  Dose computation on Pinnacle                    |
#\.................................................*/

if [ ${exclude} != 0 ]; then
echo "Calculating janus plan 1 on Pinnacle"
echo beams2trial "${PINDIR}/Patient_${pin1Id}/Plan_${pin1Plan}/plan.Trial" \
                 "${PINDIR}/Patient_${pin1Id}/Plan_${pin1Plan}/plan.Points" \
                 "${jodir1}/b" \
                 "${jodir1}/plan_im" \
                 "${PINDIR}/Patient_${pin1Id}/ImageSet_${pin1Img}.header" \
                 -w "${jodir1}/${w5}" \
                 -grid "${jodir1}/grid"

echo
echo "Calculating janus plan 2 on Pinnacle"
echo
echo beams2trial "${PINDIR}/Patient_${pin2Id}/Plan_${pin2Plan}/plan.Trial" \
                 "${PINDIR}/Patient_${pin2Id}/Plan_${pin2Plan}/plan.Points" \
                 "${jodir2}/b" \
                 "${jodir2}/plan_im" \
                 "${PINDIR}/Patient_${pin2Id}/ImageSet_${pin2Img}.header" \
                 -w "${jodir2}/${w6}" \
                 -grid "${jodir2}/grid"

echo
echo "Calculating janus plan 3 on Pinnacle"
echo
echo beams2trial "${PINDIR}/Patient_${pin4Id}/Plan_${pin4Plan}/plan.Trial" \
                 "${PINDIR}/Patient_${pin4Id}/Plan_${pin4Plan}/plan.Points" \
                 "${jodir3}/b" \
                 "${jodir3}/plan_im" \
                 "${PINDIR}/Patient_${pin4Id}/ImageSet_${pin4Img}.header" \
                 -w "${jodir3}/${w7}" \
                 -grid "${jodir3}/grid"

echo "Is everything OK? (Ctrl+c to exit)"
read answer
beams2trial "${PINDIR}/Patient_${pin1Id}/Plan_${pin1Plan}/plan.Trial" \
            "${PINDIR}/Patient_${pin1Id}/Plan_${pin1Plan}/plan.Points" \
            "${jodir1}/b" \
            "${jodir1}/plan_im" \
            "${PINDIR}/Patient_${pin1Id}/ImageSet_${pin1Img}.header" \
            -w "${jodir1}/${w5}" \
            -grid "${jodir1}/grid"

beams2trial "${PINDIR}/Patient_${pin2Id}/Plan_${pin2Plan}/plan.Trial" \
            "${PINDIR}/Patient_${pin2Id}/Plan_${pin2Plan}/plan.Points" \
            "${jodir2}/b" \
            "${jodir2}/plan_im" \
            "${PINDIR}/Patient_${pin2Id}/ImageSet_${pin2Img}.header" \
            -w "${jodir2}/${w6}" \
            -grid "${jodir2}/grid"
beams2trial "${PINDIR}/Patient_${pin4Id}/Plan_${pin4Plan}/plan.Trial" \
            "${PINDIR}/Patient_${pin4Id}/Plan_${pin4Plan}/plan.Points" \
            "${jodir3}/b" \
            "${jodir3}/plan_im" \
            "${PINDIR}/Patient_${pin4Id}/ImageSet_${pin4Img}.header" \
            -w "${jodir3}/${w7}" \
            -grid "${jodir3}/grid"
fi

echo "Press enter when the calculations are done"
read answer

#trial2dose <trial>  <image header>  <plan_im>
#     [ -t <trial number (1)> ]
#     [ -grid <shoebox> ]

echo 
echo "Retrieving janus plan 1 from pinnacle"
echo trial2dose "${PINDIR}/Patient_${pin1Id}/Plan_${pin1Plan}/plan.Trial" \
                "${PINDIR}/Patient_${pin1Id}/ImageSet_${pin1Img}.header" "${jodir1}/plan_im" \
                -grid "${jodir1}/grid" -d "${jodir1}/g"
echo
echo "Retrieving janus plan 2 from pinnacle"
echo trial2dose "${PINDIR}/Patient_${pin2Id}/Plan_${pin2Plan}/plan.Trial" \
                "${PINDIR}/Patient_${pin2Id}/ImageSet_${pin2Img}.header" "${jodir2}/plan_im" \
                -grid "${jodir2}/grid" -d "${jodir2}/g"
echo
echo "Retrieving janus plan 3 from pinnacle"
echo trial2dose "${PINDIR}/Patient_${pin4Id}/Plan_${pin4Plan}/plan.Trial" \
                "${PINDIR}/Patient_${pin4Id}/ImageSet_${pin4Img}.header" "${jodir3}/plan_im" \
                -grid "${jodir3}/grid" -d "${jodir3}/g"

trial2dose "${PINDIR}/Patient_${pin1Id}/Plan_${pin1Plan}/plan.Trial" \
           "${PINDIR}/Patient_${pin1Id}/ImageSet_${pin1Img}.header" "${jodir1}/plan_im" \
           -grid "${jodir1}/grid" -d "${jodir1}/g"

trial2dose "${PINDIR}/Patient_${pin2Id}/Plan_${pin2Plan}/plan.Trial" \
            "${PINDIR}/Patient_${pin2Id}/ImageSet_${pin2Img}.header" "${jodir2}/plan_im" \
            -grid "${jodir2}/grid" -d "${jodir2}/g"

trial2dose "${PINDIR}/Patient_${pin4Id}/Plan_${pin4Plan}/plan.Trial" \
           "${PINDIR}/Patient_${pin4Id}/ImageSet_${pin4Img}.header" "${jodir3}/plan_im" \
           -grid "${jodir3}/grid" -d "${jodir3}/g"

compDose "${jodir1}"
cp -v ${jodir1}/sum ${jodir1}/${s5}

compDose "${jodir2}"
cp -v ${jodir2}/sum ${jodir2}/${s6}

compDose "${jodir3}"
cp -v ${jodir3}/sum ${jodir3}/${s7}


#/*`````````````````````````````````````````````````\
#|  Dose summation on CT4                           |
#\.................................................*/

mkdir ${odir4} || wrong "Can not create ${odir4} directory"
cd "${odir4}" || exit 1
ln -s "../a"
ln -s "../patient_name"
ln -s "../plan_im"
mkdir b v g
cd "${cpwd}" || exit 1

echo "Created ${odir4}"

# Fourth dose grid: (can copy, or) use boxana
#cp -p "$d4/$p4/grid" "$odir4/grid"
cp -p "${d4}/grid" "${odir4}/grid"
echo "grid around skin created"

# The 6th dose grid (already on CT4)
cp -p "${odir4}/grid" "${odir4}/g/af"
cp -vp "${jodir3}/${s7}" "${odir4}/g/af"
echo "copy of 3.5->4 done"

# The 5th dose grid (on CT3): needs transform 4->3, using inversion from 3->4
cp -p "${odir4}/grid" "${odir4}/g/ae"
deformDose "${odir4}/g/ae" "${v4to3}" "${d3}/${p3}/${s3}" \
    -ct1 "${d3}/${i3}" -pi1 "${d3}/plan_im" \
    -ct2 "${d4}/${i4}" -pi2 "${d4}/plan_im"
echo "deformDose 3->4 done"

# The 4th dose grid (on CT2): need transform 4->2, using inversion from 2->3 and 3->4
cp -p "${odir4}/grid" "${odir4}/g/ad"
deformDose "${odir4}/g/ad" "${v4to2}" "${jodir2}/${s6}" \
  -ct1 "${d2}/${i2}" -pi1 "${d2}/plan_im" \
  -ct2 "${d4}/${i4}" -pi2 "${d4}/plan_im"
echo "deformDose 2.5->4 done"

# The 3th dose grid (on CT2): need transform 4->2, using inversion from 2->3 and 3->4
cp -p "${odir4}/grid" "${odir4}/g/ac"
deformDose "${odir4}/g/ac" "${v4to2}" "${d2}/${p2}/${s2}" \
  -ct1 "${d2}/${i2}" -pi1 "${d2}/plan_im" \
  -ct2 "${d4}/${i4}" -pi2 "${d4}/plan_im"
echo "deformDose 2->4 done"

# The 2th dose grid (on CT1): need transform 4->1, using inversions from 1->2, 2->3 and 3->4
cp -p "${odir4}/grid" "${odir4}/g/ab"
deformDose "${odir4}/g/ab" "${v4to1}" "${jodir1}/${s5}" \
  -ct1 "${d1}/${i1}" -pi1 "${d1}/plan_im" \
  -ct2 "${d4}/${i4}" -pi2 "${d4}/plan_im"
echo "deformDose 1.5->4 done"

# First dose grid: need transform 4->1, using inversions from 1->2, 2->3 and 3->4
cp -p "${odir4}/grid" "${odir4}/g/aa"
deformDose "${odir4}/g/aa" "${v4to1}" "${d1}/${p1}/${s1}" \
  -ct1 "${d1}/${i1}" -pi1 "${d1}/plan_im" \
  -ct2 "${d4}/${i4}" -pi2 "${d4}/plan_im"
echo "deformDose 1->4 done"

# Combine all dose grid together
#
# Combine
canbeam "${odir4}/b/aa" "${d4}/a/skin" 25062 "${d4}/a/skin" 10 10
cp -p "${odir4}/b/aa" "${odir4}/b/ab"
cp -p "${odir4}/b/aa" "${odir4}/b/ac"
cp -p "${odir4}/b/aa" "${odir4}/b/ad"
cp -p "${odir4}/b/aa" "${odir4}/b/ae"
cp -p "${odir4}/b/aa" "${odir4}/b/af"
list=".xplandisp_vis .xvsim_vis logfile"
for i in ${list}; do
  if [ -f "${d3}/${p3}/${i}" ]; then
    cp -p "${d3}/${p3}/${i}" "${odir4}/${i}"
  fi
done

cd "${odir4}" || exit 1

echo "${n1}" "${n2}" "${n3}" "${n5}" "${n6}" "${n7}" | gawk '//{
  n=split($0,a); for(i=1;i<=n;++i) printf " %g",0.01*a[i]; }' | asc2wt "wt"
cp -p "wt" "wt_total"
cp -p "wt" "wt_opt"

# interpolate dose grids in the g directory based on the grid file
for i in `ls g`; do
    grinterpol g/$i  grid  g/$i
done

construct_points
vdose

touch v/*
sleep 1
touch g/*

# Sum all grid files to one sum file
gList=""
for i in `ls g`; do
    gList="$gList -g g/$i"
done
echo "Sum grid files from g directory to sum file"
echo "sum_grids_sep -w wt -o sum ${gList}"
sum_grids_sep -w wt -o sum ${gList}

cd "${cpwd}" || exit 1


echo "Calculation done"
